package com.ddn.service.logistics.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.ddn.mapper.logistics.HqRepairMapper;
import com.ddn.mapper.logistics.dto.HqRepairExportDto;
import com.ddn.model.logistics.HqRepair;
import com.ddn.service.logistics.HqRepairService;
import com.ddn.util.StringUtil;
import com.ddn.vo.PageBean;
import com.ddn.vo.R;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;


/**
 * @author zlp
 * @date 2022/8/29 17:45
 */
@Service
public class HqRepairServiceImpl implements HqRepairService {
    @Autowired
    private HqRepairMapper dao;


    @Override
    public R save(HqRepair repair) {
        if (dao.save(repair) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @Override
    public R delete(int id) {
        if(dao.delete(id)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public R update(HqRepair repair) {
        if (dao.update(repair) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }
    @Override
    public PageBean pageByName(String departname, int page, int limit) {
        if(StringUtil.isNotEmpty(departname)){
            departname="%"+departname+"%";
        }
        int start=(page-1)*limit;
        return  new PageBean((int) dao.selectCount(departname),dao.selectPage(departname,start,limit));
    }

    @Override
    public List<HqRepairExportDto> all() {
        return dao.all();
    }

    @Override
    public R upload(MultipartFile file) throws IOException {
       if(!file.isEmpty()){
           List<HqRepair> list= EasyExcel.read(file.getInputStream(), HqRepair.class, new ReadListener() {
               @Override
               public void invoke(Object o, AnalysisContext analysisContext) {
               }
               @Override
               public void doAfterAllAnalysed(AnalysisContext analysisContext) {
               }
           }).sheet().doReadSync();
           if(dao.saveBatch(list)>0){
               return  R.ok();
           }
       }
      return R.fail();
    }

    @Override
    public R delByIds(int[] ids) {
        if (dao.delByIds(ids) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }
}
